require 'progressbar'

service = "blogger"
extension = "html"
dir_out_path = ARGV[0]

crawl_table = Object.const_get("#{service.capitalize}Crawl")

pbar = ProgressBar.new("#{crawl_table.table_name}", 100)
last_progress_value = 0
progress_value = last_progress_value
records_analyzed = 0
count = crawl_table.count_by_sql "SELECT count(*) FROM #{crawl_table.table_name} WHERE status = 200"

crawl_table.find_each (:conditions => "status = 200") { |c| 
  outfile = File.open("#{dir_out_path}/#{c.blogspot_crawls[0].service.user.ff_id}.#{extension}", 'wb')
  outfile.write c.body
  
  records_analyzed += 1
  progress_value = sprintf("%d", records_analyzed.to_f / count * 100.0).to_i
  if  progress_value != last_progress_value then
    last_progress_value = progress_value
    pbar.set progress_value
  end
  
  outfile.close
}

puts pbar.finish